import hashlib
import re

import redis
import scrapy
from ..items import MyFileItem
from ..settings import REDIS_CONNECT


class Ssosg(scrapy.Spider):
    name = 'sso_sup_gov_sg'
    allowed_domains = ['supremecourt.gov.sg']
    # 法案第一页地址
    start_urls = [
        'https://www.supremecourt.gov.sg/news/supreme-court-judgments/page/1',
    ]
    id = 0

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.pool = redis.ConnectionPool(host=REDIS_CONNECT['REDIS_HOST'],
                                         port=REDIS_CONNECT['REDIS_PORT'],
                                         password=REDIS_CONNECT['REDIS_PASSWD'],
                                         db=REDIS_CONNECT['REDIS_DB'], decode_responses=True)

    def parse(self, response):
        # 获得每页列表
        trs = response.xpath('//*[@id="ContentPlaceHolderContent_C008_DivCode"]/ul[@class="judgmentblock"]/li')
        for tr in trs:

            # 判例关键词
            keywordS = tr.xpath('.//*[@class="text"]/small/text()')
            keyword = ''
            if len(keywordS):
                keyword = keywordS.get().strip()
            # 判例标题
            titleS = tr.xpath('.//*[@class="text"]/text()')
            title = ''
            if len(titleS):
                for titleSl in titleS:
                    title = title + ' ' + titleSl.get().strip()
                title = re.sub(r'''^ *''', r'''''', title)  # 去除开始空格
            # 判例编码
            sparefieldS = tr.xpath('.//*[@class="text"]/ul[@class="decision"]/li[1]/text()')
            sparefield = ''
            if len(sparefieldS):
                sparefield = sparefieldS.get().strip()
            # 判决日期
            judgementdateS = tr.xpath('.//*[@class="text"]/ul[@class="decision"]/li[2]/text()')
            judgementdate = ''
            if len(judgementdateS):
                judgementdate = judgementdateS.get().strip()
            # 备用字段
            casecodeS = tr.xpath('.//*[@class="text"]/ul[@class="decision"]/li[3]/text()')
            casecode = ''
            if len(casecodeS):
                casecode = casecodeS.get().strip()
            # 摘要详细网址
            abstractUrlS = tr.xpath('.//*[@class="download"]//*[@class="doc-download"]/@href')
            abstractUrl = ''
            if len(abstractUrlS):
                abstractUrl = 'https://www.' + self.allowed_domains[0] + abstractUrlS.get().strip()
            # 判例全文下载网址
            downloadUrlS = tr.xpath('.//*[@class="download"]//*[@class="pdf-download"]/@href')
            downloadUrl = ''
            if len(downloadUrlS):
                downloadUrl = 'https://www.' + self.allowed_domains[0] + downloadUrlS.get().strip()
            if len(downloadUrl):
                item = MyFileItem()
                # 国别站点等信息
                country = 'Singapore'
                website = 'supremecourt'
                modular = 'case'
                # 唯一ID
                systemid = str(hashlib.md5(downloadUrl.encode('utf-8')).hexdigest())
                rediscursor = redis.Redis(connection_pool=self.pool)
                try:
                    right = rediscursor.sismember(REDIS_CONNECT['REDIS_KEY'], systemid)
                except:
                    raise redis.RedisError
                if right:
                    continue

                # 下载文件名
                fina = ''
                if len(downloadUrl) > 0:
                    fina = 'f' + str(hashlib.md5(downloadUrl.encode('utf-8')).hexdigest())
                item['fina'] = fina

                item['file_urls'] = ''
                item['country'] = country
                item['website'] = website
                item['modular'] = modular
                item['ext'] = 'pdf'
                item['fina'] = fina
                item['title'] = title
                item['abstractUrl'] = ''
                item['abstract'] = ''
                item['detailUrl'] = ''
                item['detail'] = ''
                item['downloadUrl'] = downloadUrl

                item['Title'] = title
                item['CaseNumber'] = casecode
                item['KeyWord'] = keyword
                item['SortA'] = 'LAWCOUNTRYXJP'
                item['People'] = ''
                item['CaseOfAction'] = ''
                item['UseLaw'] = ''
                item['AdjudicationDate'] = judgementdate
                item['FullText'] = ''
                item['JudgAgency'] = ''
                item['SortB'] = 'LANGUAGEYY'
                item['SortC'] = ''
                item['CaseSummary'] = ''
                item['Articles'] = ''
                item['Chapter'] = ''
                item['Section'] = ''
                item['SYS_FLD_DIGITFILENAME'] = fina
                item['FileUrl'] = ''
                item['AbstractFileName'] = ''
                item['DownLoadUrl'] = downloadUrl
                item['DownLoadWebNameC'] = '新加坡最高法院'
                item['DownLoadWebNameE'] = "SUPPEMECOURT SINGAPORE"
                item['SYSID'] = systemid
                item['Website'] = 'Supreme court judgments'
                item['Isconversion'] = '0'
                item['CaseDate'] = ''

                yield item

        # 翻页
        for a in response.css('#ctl00_ContentPlaceHolderContent_C008_RdpJudgmentsPager_ctl02_NextButton'):
            yield response.follow(a, callback=self.parse)
            continue

